/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.core.fcomps.delegate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.jbfilter.core.CleaningStrategy;
import org.jbfilter.core.EnableStrategy;
import org.jbfilter.core.fcomps.single.SingleFilterComponent;

/**
 * @author Marcus Adrian
 * @see FilterComponentDelegate
 * @see SingleFilterComponent
 */
public class SingleFilterComponentDelegate<E, T>
	implements
		SingleFilterComponent<E, T> {

	private List<SingleFilterComponent<E, T>> sfComps = new ArrayList<SingleFilterComponent<E,T>>();
	private FilterComponentDelegate<E> fCompDelegate = new FilterComponentDelegate<E>();
	
	public void add(SingleFilterComponent<E, T> filterComponent) {
		sfComps.add(filterComponent);
		fCompDelegate.add(filterComponent);
	}
	public void addAll(Collection<SingleFilterComponent<E, T>> filterComponents) {
		for (SingleFilterComponent<E, T> filterComponent : filterComponents) {
			add(filterComponent);
		}
	}
	@Override
	public void setEnabled(boolean enabled) {
		fCompDelegate.setEnabled(enabled);
	}
	@Override
	public boolean isEnabled() {
		return fCompDelegate.isEnabled();
	}
	@Override
	public boolean isActive() {
		return fCompDelegate.isActive();
	}
	@Override
	public void clean() {
		fCompDelegate.clean();
	}
	@Override
	public void setCleaningStrategy(CleaningStrategy cleaningStrategy) {
		fCompDelegate.setCleaningStrategy(cleaningStrategy);
	}
	@Override
	public String getId() {
		return fCompDelegate.getId();
	}
	@Override
	public void setId(String id) {
		fCompDelegate.setId(id);
	}
	@Override
	public boolean isInverted() {
		return fCompDelegate.isInverted();
	}
	@Override
	public void setInverted(boolean inverted) {
		fCompDelegate.setInverted(inverted);
	}
	@Override
	public void setEnableStrategy(EnableStrategy enableStrategy) {
		fCompDelegate.setEnableStrategy(enableStrategy);
	}
	@Override
	public CleaningStrategy getCleaningStrategy() {
		return fCompDelegate.getCleaningStrategy();
	}
	@Override
	public EnableStrategy getEnableStrategy() {
		return fCompDelegate.getEnableStrategy();
	}
	@Override
	public T getValue() {
		return getFirst().getValue();
	}
	@Override
	public void setValue(T value) {
		for (SingleFilterComponent<E, T> sfComp: sfComps) {
			sfComp.setValue(value);
		}
	}
	@Override
	public T getValueCleanValue() {
		return getFirst().getValueCleanValue();
	}
	@Override
	public void setValueCleanValue(T valueCleanValue) {
		for (SingleFilterComponent<E, T> sfComp: sfComps) {
			sfComp.setValueCleanValue(valueCleanValue);
		}
	}
	private SingleFilterComponent<E, T> getFirst() {
		for (SingleFilterComponent<E, T> sfComp: sfComps) {
			return sfComp;
		}
		throw new IllegalStateException("No SingleFilterComponent set.");
	}
}
